//
//  ZXBotuiqueSpecialViewController.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXBotuiqueSpecialViewController.h"

#import "ZXDestinationViewViewController.h"
#import "ZXBoutiqueSpecialViewModel.h"

#import "ZXBoutiqueCollectionViewCell.h"

#import "CXRefreshStateHeader.h"

static NSString * const kCell = @"BoutiqueCell";

@interface ZXBotuiqueSpecialViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) ZXBoutiqueSpecialViewModel * botiqueModel;

@property (nonatomic, strong) NSString * type;

@property (nonatomic, assign) NSInteger categoryID;

@end

@implementation ZXBotuiqueSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNavBar];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.collectionView.mj_header beginRefreshing];
    
    
}

#pragma mark - collectionView Data
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(DEVICE_SCREEN_WIDTH - 20, 100);
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.botiqueModel.section;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ZXBoutiqueCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
    
    [cell.icoImgView sd_setImageWithURL:[self.botiqueModel coverURLForSection:indexPath.section index:indexPath.item] placeholderImage:[UIImage imageNamed:@"cell_bg_niData_2"]];
    cell.titleLabel.text = [self.botiqueModel titleForSection:indexPath.section index:indexPath.item];
    cell.commentsCountLabel.text = [NSString stringWithFormat:@"%ld", [self.botiqueModel commentsCountForSection:indexPath.section index:indexPath.item]];
    cell.priceLabel.text = [NSString stringWithFormat:@"%ld", [self.botiqueModel priceForSection:indexPath.section index:indexPath.item]];
    cell.displayPriceLabel.text = [self.botiqueModel displayVipPriceForSection:indexPath.section index:indexPath.item];
    
    return cell;
}

#pragma mark - collectionViewCell 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);

    ZXDestinationViewViewController * destinationVC = [[ZXDestinationViewViewController alloc] initWithAlumId:[self.botiqueModel albumIdForSection:indexPath.section index:indexPath.item] title:[self.botiqueModel mainTitleForSection:indexPath.item]];
    
    [self.navigationController pushViewController:destinationVC animated:YES];
    
}

#pragma mark - initNavBar
- (void)setupNavBar {

    self.navigationItem.title = @"首页";
    
    // 历史
    UIBarButtonItem * settingItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"myCenterFilterNormal"] highlightedImage:[UIImage imageNamed:@"myCenterFilterSelected"] target:self action:@selector(setting)];
    
    // 下载
    UIBarButtonItem * nightItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"search_btn_nl"] highlightedImage:[UIImage imageNamed:@"search_btn_nl_hl"] target:self action:@selector(night:)];
    
    self.navigationItem.rightBarButtonItems = @[nightItem, settingItem];
}

#pragma mark - 点击
- (void)setting {
    NSLog(@"点击了历史按钮");
}

- (void)night:(UIBarButtonItem *)btn {

    NSLog(@"点击了下载按钮");
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView {

    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 1.0f;
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.itemSize = CGSizeMake(DEVICE_SCREEN_WIDTH, 125);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 3, 10);
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT) collectionViewLayout:flowLayout];
        [self.view addSubview:collectionView];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        collectionView.backgroundColor = [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZXBoutiqueCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:kCell];
        
        collectionView.bounces = YES;
        self.collectionView = collectionView;
        
        // 上拉刷新
        collectionView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.botiqueModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];

        }];
    }
    return _collectionView;
}

- (ZXBoutiqueSpecialViewModel *)botiqueModel {

    if (!_botiqueModel) {
        _botiqueModel = [[ZXBoutiqueSpecialViewModel alloc] init];
    }
    return _botiqueModel;
}

@end
